<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY nbsp "&#x00A0;">
]>
<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:msxml="urn:schemas-microsoft-com:xslt"
  xmlns:site.library="urn:site.library"
  xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
  exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets site.library">
  <xsl:import href="_common.xslt"/>
  
  <xsl:output method="xml" omit-xml-declaration="yes" indent="no"/>
  
  <xsl:param name="currentPage"/>
  <xsl:variable name="SiteInfo" select="site.library:PageSite($currentPage/@id)"/>
  <xsl:variable name="Site" select="umbraco.library:GetXmlNodeById($SiteInfo/node/@id)"/>
  
  <xsl:variable name="selPagesIds" select="$currentPage/ancestor-or-self::*/@id"/>
  <xsl:variable name="selPages" select="$currentPage/ancestor-or-self::*"/>
  
  <xsl:variable name="isEditMode" select="site.library:IsCanvas()"/>

  <xsl:template match="/">
    <div class="main-menu">
      <ul class="nav main-nav">
        <!-- homepage & rest first level pages -->
        <xsl:call-template name="RenderChilds">
          <xsl:with-param name="Parent" select="$Site"/>
          <xsl:with-param name="level" select="1"/>
        </xsl:call-template>
      </ul>
    </div>
  </xsl:template>
  
  <xsl:template name="RenderChilds">
    <xsl:param name="Parent" select="false"/>
    <xsl:param name="level"/>
    <xsl:variable name="Childs" select="$Parent/*[@isDoc and string(hideInMenu) != '1']"/>
    <ul class="subnav">
      <xsl:choose>
        <xsl:when test="count($Childs)>0">
          <xsl:for-each select="$Childs">
            <xsl:variable name="Child" select="current()"/>
            <xsl:variable name="isCISelected" select="count($selPagesIds[number(.)=number($Child/@id)])>0"/>
            <xsl:call-template name="RenderItem">
              <xsl:with-param name="Page" select="$Child"/>
              <xsl:with-param name="level" select="number($level)-1"/>
              <xsl:with-param name="exClass"><xsl:if test ="$isCISelected"> active</xsl:if><xsl:if test="position()=last()"> last</xsl:if><xsl:if test="position()=1"> first</xsl:if></xsl:with-param>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:when>
        <xsl:otherwise><li><ins><xsl:call-template name="comment"/></ins></li></xsl:otherwise>
      </xsl:choose>
    </ul>
  </xsl:template>

  <xsl:template name="RenderItem">
    <xsl:param name="Page" select="false"/>
    <xsl:param name="isSelected" select="false"/>
    <xsl:param name="exClass"/>
    <xsl:param name="level"/>
    <xsl:variable name="itemUrl"><xsl:choose><xsl:when test="$Page/navigateUrl and $Page/navigateUrl != ''"><xsl:value-of select="$Page/navigateUrl"/></xsl:when><xsl:otherwise><xsl:value-of select="umbraco.library:NiceUrl($Page/@id)"/></xsl:otherwise></xsl:choose></xsl:variable>

    <li class="{$exClass} menuitem_{position()} ">
      <xsl:if test="string-length($Page/menuDescription)>0"><xsl:attribute name="title"><xsl:value-of disable-output-escaping="yes" select="$Page/menuDescription"/></xsl:attribute></xsl:if>
      <a href="{$itemUrl}">
        <xsl:if test="string($Page/target) != ''"><xsl:attribute name="target"><xsl:value-of select="$Page/target"/></xsl:attribute></xsl:if>
        <xsl:if test="$isEditMode"><xsl:attribute name="onclick">showEditNodeDialog('<xsl:value-of disable-output-escaping="yes" select="$Page/@id"/>', '<xsl:value-of disable-output-escaping="yes" select="$Page/title"/>');return false;</xsl:attribute></xsl:if>
        <xsl:if test="string($Page/menuImage) != ''"><img alt="{$Page/@nodeName}" src="{umbraco.library:GetMedia($Page/menuImage, 0)/umbracoFile}"/></xsl:if>
        <xsl:if test="not (string($Page/menuImageOnly) = '1')">
          <!--show [title] or [menu title]-->
          <xsl:value-of select="site.library:ShortIf(string-length($Page/menuTitle)>0,$Page/menuTitle,site.library:ShortIf(string-length($Page/title)>0,$Page/title,$Page/@nodeName))" disable-output-escaping="yes"/>
        </xsl:if>
        <ins><xsl:call-template name="comment"/></ins>
      </a>
      <xsl:if test="number($level)>0">
        <xsl:call-template name="RenderChilds">
          <xsl:with-param name="Parent" select="$Page"/>
          <xsl:with-param name="level" select="number($level)-1"/>
        </xsl:call-template>
      </xsl:if>
    </li>
  </xsl:template>

</xsl:stylesheet>